/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.xxl.adapter.dao;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.xxl.adapter.core.XxlConvert;
import com.xxl.adapter.entity.JobGroup;
import com.xxl.adapter.repository.JobGroupRepository;
import com.xxl.job.admin.core.model.XxlJobGroup;
import com.xxl.job.admin.dao.XxlJobGroupDao;
import java.util.List;
import javax.persistence.criteria.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Component;

/**
 * XxlJobGroupDaoImpl
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@Slf4j
@Component
public class JobGroupDaoImpl implements XxlJobGroupDao, XxlConvert {
  @Autowired private JobGroupRepository repository;

  @Override
  public List<XxlJobGroup> findAll() {
    return this.convert(XxlJobGroup.class, repository.findAll());
  }

  @Override
  public List<XxlJobGroup> findByAddressType(int i) {
    return this.convert(XxlJobGroup.class, repository.findAllByAddressType(i));
  }

  @Override
  public int save(XxlJobGroup xxlJobGroup) {
    JobGroup jobGroup = repository.save(this.convert(JobGroup.class, xxlJobGroup));
    xxlJobGroup.setId(jobGroup.getId());
    return jobGroup.getId();
  }

  @Override
  public int update(XxlJobGroup xxlJobGroup) {
    return this.save(xxlJobGroup);
  }

  @Override
  public int remove(int id) {
    repository.deleteById(id);
    return id;
  }

  @Override
  public XxlJobGroup load(int id) {
    return this.convert(XxlJobGroup.class, repository.findById(id).orElse(null));
  }

  public Page<JobGroup> page(int offset, int pagesize, String appname, String title) {
    return repository.findAll(
        ((root, query, cb) -> {
          Predicate predicate = cb.conjunction();
          if (StrUtil.isNotBlank(appname)) {
            predicate.getExpressions().add(cb.like(root.get("appname"), "%" + appname + "%"));
          }
          if (StrUtil.isNotBlank(title)) {
            predicate.getExpressions().add(cb.like(root.get("title"), "%" + title + "%"));
          }

          return predicate;
        }),
        PageRequest.of(
            offset,
            pagesize,
            Sort.by(
                CollectionUtil.newArrayList(
                    Order.asc("appname"), Order.asc("title"), Order.asc("id")))));
  }

  @Override
  public List<XxlJobGroup> pageList(int offset, int pagesize, String appname, String title) {
    Page<JobGroup> page = this.page(offset, pagesize, appname, title);
    return this.convert(XxlJobGroup.class, page.getContent());
  }

  @Override
  public int pageListCount(int offset, int pagesize, String appname, String title) {
    Page<JobGroup> page = this.page(offset, pagesize, appname, title);
    return (int) page.getTotalElements();
  }

  @Override
  public Logger getLogger() {
    return log;
  }
}
